﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using DevTrends.MvcDonutCaching;
using KMF.Common;
using KMF.DAL;
using KMF.Utility;
using KMF.WebUI.CustomHelpers;
using KMF.WebUI.Infrastructure;
using System.Security.Cryptography;
using KMF.WebUI.Models;

namespace KMF.WebUI.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            var keyw = Utility.Common.returnConfig("KeyWeb");
            var url = Request.Url;
            if (url != null && !url.Host.Contains("localhost"))
            {
                if (Session[SessionConfig.WebsiteSecurity] == null)
                {
                    var md5 = new MD5CryptoServiceProvider();
                    var utf8 = new UTF8Encoding();
                    var dmn = url.Host;
                    var keyCurrent =
                        BitConverter.ToString(
                            md5.ComputeHash(utf8.GetBytes(dmn + "07-2E-49-5F-DC-76-E5-9F-C5-BB-41-55-C3-46-DE-A8")));
                    // chek domain 
                    if (!keyw.Equals(keyCurrent))
                    {
                        return RedirectToAction("IndexSecurity");                
                    }
                }
                
                ViewBag.Canonical = url.AbsoluteUri;
            }

            var model = FunctionHelpers.GetWebInfo();

            return View(model);
        }

        public ActionResult IndexSecurity()
        {
            return PartialView();
        }
        public ActionResult Header(int id = 1, string newsId = "", string cateId = "")
        {
            var model = ProductCategoryDb.GetMenuLeftOnCategoryHome() ?? new List<ProductCategoryMenuLeftDto>();
            ViewBag.pCate = cateId;
            ViewBag.News = NewsDb.GetTitleByNewscategory(id) ?? new List<NewsTitleDto>();
            ViewBag.CateId = id;
            ViewBag.NewsId = newsId;
            return PartialView(model);
        }

        public ActionResult NewProduct()
        {
            var model = ProductDb.GetListAllProductNewHome() ?? new List<ProductRateByCateTopListPagingDto>();
            return PartialView(model);
        }
        [HttpPost]
        public ActionResult NewProduct(ProductOrderInfo pInfo)
        {
            if (Session[SessionConfig.SessionCartInfor] == null)
            {
                var cartInfor = new OrderInfo { ProductOrderList = new List<ProductOrderInfo> { pInfo } };
                cartInfor.TotalMoney = pInfo.PromotionPrice > 0 ? pInfo.PromotionPrice * pInfo.ProductQuantity : pInfo.RootPriceProduct * pInfo.ProductQuantity;
                Session[SessionConfig.SessionCartInfor] = cartInfor;
            }
            else
            {
                var cartInfor = (OrderInfo)Session[SessionConfig.SessionCartInfor];
                cartInfor = FunctionHelpers.CalTotalOrder(cartInfor, pInfo);
                Session[SessionConfig.SessionCartInfor] = cartInfor;
            }

            // Check login
            return RedirectToAction("Finish", "Order");
        }
        public ActionResult DiscountProduct()
        {
            var model = ProductDb.GetAllProductByCategoryId_Filter_Discount() ?? new List<ProductRateByCateTopListPagingDto>();
            return PartialView(model);
        }
        [HttpPost]
        public ActionResult DiscountProduct(ProductOrderInfo pInfo)
        {
            if (Session[SessionConfig.SessionCartInfor] == null)
            {
                var cartInfor = new OrderInfo { ProductOrderList = new List<ProductOrderInfo> { pInfo } };
                cartInfor.TotalMoney = pInfo.PromotionPrice > 0 ? pInfo.PromotionPrice * pInfo.ProductQuantity : pInfo.RootPriceProduct * pInfo.ProductQuantity;
                Session[SessionConfig.SessionCartInfor] = cartInfor;
            }
            else
            {
                var cartInfor = (OrderInfo)Session[SessionConfig.SessionCartInfor];
                cartInfor = FunctionHelpers.CalTotalOrder(cartInfor, pInfo);
                Session[SessionConfig.SessionCartInfor] = cartInfor;
            }

            // Check login
            return RedirectToAction("Finish", "Order");
        }
        public ActionResult ALLNewProduct()
        {
            int totalRecord = 0;
            var model = ProductDb.GetAllNewProduct(ref totalRecord) ?? new List<ProductRateByCateTopListPagingDto>();
            ViewBag.TotalRecod = totalRecord;
            return PartialView(model);
        }
        [HttpPost]
        public ActionResult ALLNewProduct(ProductOrderInfo pInfo)
        {
            if (Session[SessionConfig.SessionCartInfor] == null)
            {
                var cartInfor = new OrderInfo { ProductOrderList = new List<ProductOrderInfo> { pInfo } };
                cartInfor.TotalMoney = pInfo.PromotionPrice > 0 ? pInfo.PromotionPrice * pInfo.ProductQuantity : pInfo.RootPriceProduct * pInfo.ProductQuantity;
                Session[SessionConfig.SessionCartInfor] = cartInfor;
            }
            else
            {
                var cartInfor = (OrderInfo)Session[SessionConfig.SessionCartInfor];
                cartInfor = FunctionHelpers.CalTotalOrder(cartInfor, pInfo);
                Session[SessionConfig.SessionCartInfor] = cartInfor;
            }

            // Check login
            return RedirectToAction("Finish", "Order");
        }
        public ActionResult ALLDiscountProduct()
        {
            int totalRecord = 0;
            var model = ProductDb.GetAllDiscount(ref totalRecord) ?? new List<ProductRateByCateTopListPagingDto>();
            ViewBag.TotalRecod = totalRecord;
            return PartialView(model);
        }
        [HttpPost]
        public ActionResult ALLDiscountProduct(ProductOrderInfo pInfo)
        {
            if (Session[SessionConfig.SessionCartInfor] == null)
            {
                var cartInfor = new OrderInfo { ProductOrderList = new List<ProductOrderInfo> { pInfo } };
                cartInfor.TotalMoney = pInfo.PromotionPrice > 0 ? pInfo.PromotionPrice * pInfo.ProductQuantity : pInfo.RootPriceProduct * pInfo.ProductQuantity;
                Session[SessionConfig.SessionCartInfor] = cartInfor;
            }
            else
            {
                var cartInfor = (OrderInfo)Session[SessionConfig.SessionCartInfor];
                cartInfor = FunctionHelpers.CalTotalOrder(cartInfor, pInfo);
                Session[SessionConfig.SessionCartInfor] = cartInfor;
            }

            // Check login
            return RedirectToAction("Finish", "Order");
        }
        public ActionResult Footer()
        {
            var modelFooter = FunctionHelpers.GetWebInfo();
            return PartialView(modelFooter);
        }
    }
}
